/**
 * @author JackGao
 *			email:jackgao.china@gmail.com
 *  date：2016-6-23
 * 
 */
package cn.cuit.ds.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import cn.cuit.ds.dao.IUserinfoDao;
import cn.cuit.ds.domain.User;
import cn.cuit.ds.utils.JdbcUtils;

@Repository(value="UserinfoDao")
public class UserinfoDao implements IUserinfoDao {

	public void add(User user) {
		QueryRunner runner = JdbcUtils.getQueryRunner();//连接数据库,包括数据库的一些操作
		String sql = "insert into userinfo values(?,?,?,?,?)";
		try {
			runner.update(sql,user.getUserId(),user.getDeatil(),user.getRegisTime(),user.getUserName(),user.getPwd());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public List<User> getAll() {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "select * from userinfo where userName!='not'";
		try {
			return runner.query(sql, new BeanListHandler<User>(User.class) );
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public User findByNameAndPwd(User user) {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "SELECT * from userinfo where userName =? AND pwd=?";
		try {
			return runner.query(sql, new BeanHandler<User>(User.class),user.getUserName(),user.getPwd());//
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public User findByNameAndPwd(String userName, String pwd) {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql ="select * from userinfo where userName = ? AND pwd=?";
		try {
			return runner.query(sql,new BeanHandler<User>(User.class),userName,pwd);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	public User findByName(String userName) {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql ="select * from userinfo where userName=?";
		try {
			return runner.query(sql, new BeanHandler<User>(User.class),userName);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void deleteOneUser(String id) {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql ="update userinfo set userName='not' where userId=?";
		try {
			runner.update(sql,id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public User findById(String id) {
		// TODO Auto-generated method stub
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "SELECT * from userinfo where userId = ?";
		try {
		return	runner.query(sql, new BeanHandler<User>(User.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void update(User user) {
		// TODO Auto-generated method stub
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "update userinfo set deatil=?,regisTime=?,userName=?,pwd=? where userId=?";
		try {
			runner.update(sql,user.getDeatil(),user.getRegisTime(),user.getUserName(),user.getPwd(),user.getUserId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
